package cn.bdqn.controller;

import cn.bdqn.entity.Admin;
import cn.bdqn.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author zhangpengxiang
 * @description TODO
 * @date 2024/7/12
 */
@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/login")
    public String to_login() {
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String account, @RequestParam String password, Model model, HttpSession session){
        Admin admin = adminService.login(account, password);
        if (admin != null) {
            //登录成功
            session.setAttribute("admin", admin);
            return "redirect:/student/list";
        } else {
            //账号或密码错误
            model.addAttribute("message", "账号名或密码错误");
            return "login";
        }
    }


}
